<template>
  <view>
    <view class="u-flex user-box u-p-l-30 u-p-r-20 u-p-b-30" @click="login">
      <view class="u-m-r-10">
        <u-avatar :src="avatar" size="140"></u-avatar>
      </view>
      <view class="u-flex-1">
        <view class="u-font-40 u-p-b-20">Hi，{{ numName | loginFilter }}</view>
        <view class="u-font-35 u-tips-color">邮箱号: {{ email }}</view>
      </view>
      <view class="u-m-l-10 u-p-10">
        <u-icon name="arrow-right" color="#969799" size="28"></u-icon>
      </view>
    </view>

    <view class="u-m-t-20">
      <u-cell-group>
        <u-cell-item icon="edit-pen-fill" @click="change" title="修改个人资料"></u-cell-item>
        <!-- #ifndef H5 -->
        <u-cell-item icon="scan" title="扫一扫" @click="scan"></u-cell-item>
        <!-- #endif -->
        <u-cell-item @click="clazz" icon="heart" title="我的班级"></u-cell-item>
      </u-cell-group>
    </view>

    <view class="u-m-t-20">
      <u-cell-group>
        <u-cell-item icon="star" title="收藏" @click="collect"></u-cell-item>
        <u-cell-item icon="setting" @click="setting" title="设置"></u-cell-item>
      </u-cell-group>
    </view>

    <view class="u-m-t-20">
      <u-cell-group>
        <u-cell-item icon="chat" title="联系客户"></u-cell-item>
        <u-cell-item icon="question-circle" title="帮助"></u-cell-item>
      </u-cell-group>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      show: true,
      numName: '',
      avatar: '',
      email: ''

    };
  },
  onLoad() {
    this.initial();
  },
  onPullDownRefresh() {
  	this.initial();
	uni.stopPullDownRefresh();
  },
  methods: {
	initial(){
		uni.$emit("setVuexUserInfo",{user:null,isLoginPage: false});
		this.numName = this.$store.getters.numName;
		this.avatar = this.$store.getters.avatar;
		this.email = this.$store.getters.email;
	},
    login() {
      if (this.$store.getters.numName === null || this.$store.getters.numName === '') {
        uni.reLaunch({
          url: '/pages/login/login',
          fail: (err) => {
            this.$u.toast('跳转出错')
          }
        })
      }
    },
    clazz(){
      this.$u.route('/pages/my_class/my_class');
    },
    change(){
      this.$u.route('/pages/change_mine/change_mine');
    },
    setting(){
      this.$u.route('/pages/setting/setting');
    },
    collect(){
      this.$u.route('/pages/collect/collect');
    },
    scan() {
      let that = this;
      // 允许从相机和相册扫码
      uni.scanCode({
        success: function(res) {
          console.log('条码类型：' + res.scanType);
          console.log('条码内容：' + res.result);
          // 如果是链接直接跳转
          let reg = /^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/;
          if (reg.test(res.result)) {
            // #ifdef MP
            that.$u.toast("微信小程序不支持打开网页链接！")
            // #endif

            // #ifndef MP
            void plus.runtime.openWeb(res.result);
            // #endif
          } else {
            let result = res.result.split(":");
            if (result[0] === "feige") {
              if (result[1] === "course" || result[1] === "clazz"){
                that.$u.route({
                  url: 'pages/scan_to/scan_to',
                  params: {
                    type: result[1],
                    id: result[2]
                  }
                })
              }else if (result[1] === "sign"){
                // 发送签到信息给后端
                let data = {
                  signUserId: that.$store.getters.id,
                  signTaskId: result[2]
                }
                that.$u.api.saveSignIn(data).then(res => {
                  console.log(res);
                  if (res.code === 200) {
                    that.$u.toast('签到成功');
                  }else {
                    that.$u.toast('签到失败！')
                  }
                })
              }
            } else {
              that.$u.toast("该二维码不合法！");
            }
          }
        },
      });
    }
  },
  filters:{
    loginFilter(v){
      return v !== null && v !== '' && v !== undefined ? v : "您未登录";
    }
  }
}
</script>

<style lang="scss">

</style>
